<?php
/**
 * 基础设置
 */
namespace App;
defined('IN_IA') or exit('Access Denied');
use Model\SettingMod;
use Model\SettingModel;
use Pub\tool\Setting as SettingCache;

class Setting extends Action
{
    private $Setting = null;
    public function __construct()
    {
        parent::__construct();
        $this->Setting = new SettingMod();
    }

    /**
     * 设置参数
     * @param array data 参数
     */
    public function set()
    {
        $_setting = SettingModel::where("uniacid",$this->_uniacid)->value("id");
        if($_setting){
            $_result = SettingModel::where("uniacid",$this->_uniacid)
                ->update($this->_gpc["data"]);
        }else{
            $this->_gpc["data"]["uniacid"] = $this->_uniacid;
            $_result = SettingModel::create($this->_gpc["data"]);
        }
        if($_result) $this->_result = array("status"=>"success");
        $this->result($this->_result);
    }

    /**
     * 设置参数
     * @param array data 参数
     * @return boolean
     */
    public function setSetting($_data)
    {
        SettingCache::clear();
        $_setting = $this->Setting->findOne(array("uniacid"=>$this->_uniacid),array("id"));
        if($_setting){
            $_result = $this->Setting->update($_data,array("uniacid"=>$this->_uniacid));
        }else{
            $_data["uniacid"] = $this->_uniacid;
            $_result = $this->Setting->insert($_data);
        }
        return $_result;
    }

    public function find()
    {
        $_setting = SettingModel::where("uniacid",$this->_uniacid)
            ->field($this->_gpc["return"])
            ->cache(!KUYUAN_DEBUG)
            ->find();
        $this->result(array('status'=>'success','data'=>$_setting));
    }

    public function easyFind()
    {
        $_return = $this->_gpc['return'];
        $_setting = array();
        foreach($_return as $_v){
            $_setting[$_v] = SettingCache::get($_v);
            if($_v === "web_icon") $_setting["web_icon_url"] = tomedia($_setting[$_v]);
        }
        $this->_result = array('status'=>'success','data'=>$_setting);
        $this->result($this->_result);
    }

}